home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-04-23 | 15.5 KB | 664 lines | [TEXT/PJMM] |
- unit Setup;
- interface
-
- procedure GetNewPreferences;
- procedure HandleMenuChoice (menuChoice: LONGINT);
- implementation
- uses
- FixMath, Utility, Sane, Globals, Script, MySpeech, Watch, Daemon;
- type
- PopPrivateH = ^PopPrivatePtr;
- PopPrivatePtr = ^PopPrivate;
- PopPrivate = record
- mHandle: MenuHandle;
- mID: integer;
- charArray: char;
- end;
- const
- SETUP_ID = 128;
- OK_BUT = 1;
- CAN_BUT = 2;
- HOUR_CHECK = 3;
- DAY_CHECK = 4;
- BITES_CHECK = 5;
- TEST_BUT = 6;
- VOICE_POP = 7;
-
- PITCH_SLIDE = 8;
- WPM_SLIDE = 9;
- MOD_SLIDE = 10;
- VOL_SLIDE = 11;
-
- PITCH_TXT = 12;
- WPM_TXT = 13;
- MOD_TXT = 14;
- VOL_TXT = 15;
-
- WAIT_TIME = 20;
- BITE_FREQ_SLIDE = 22;
-
- USE_DAEMON_CHECK = 26;
- KEEP_VOICE_CHECK = 27;
-
- var
- gTrackTime: LONGINT;
- gPeriod: char;
- gLocalPrefs: PrefRecord;
-
- function SetDialogCancelItem (theDialogPtr: DialogPtr;
- newItem: integer): OSErr;
- inline
- $303C, $0305, $AA68;
- function SetDialogDefaultItem (theDialogPtr: DialogPtr;
- newItem: integer): OSErr;
- inline
- $303C, $0304, $AA68;
- procedure ActivateItem (setupDlg: DialogPtr;
- item: integer;
- activ: Boolean);
- var
- iHandle: ControlHandle;
- iType: integer;
- iRect: Rect;
- begin
- GetDItem(setupDlg, item, iType, Handle(iHandle), iRect);
- if activ then
- begin
- HiliteControl(iHandle, 0);
- end
- else
- begin
- HiliteControl(iHandle, 255);
- end;
- end;
- procedure ActivateForDaemon (setupDlg: DialogPtr;
- activ: Boolean);
- begin
-
- ActivateItem(setupDlg, VOICE_POP, activ);
- ActivateItem(setupDlg, PITCH_SLIDE, activ);
- ActivateItem(setupDlg, WPM_SLIDE, activ);
- ActivateItem(setupDlg, MOD_SLIDE, activ);
- ActivateItem(setupDlg, VOL_SLIDE, activ);
- ActivateItem(setupDlg, KEEP_VOICE_CHECK, activ);
- end;
- procedure MapValue2Fixed (ctlH: ControlHandle;
- value: integer;
- item: integer;
- var fixedValue: Fixed);
- var
- max: extended;
- min: extended;
- minFloat: extended;
- maxFloat: extended;
- result: extended;
- begin
- max := GetCtlMax(ctlH);
- min := GetCtlMin(ctlH);
- case item of
- PITCH_TXT:
- begin
- minFloat := MIN_PITCH;
- maxFloat := MAX_PITCH;
- end;
- VOL_TXT:
- begin
- minFloat := MIN_VOL;
- maxFloat := MAX_VOL;
- end;
- MOD_TXT:
- begin
- minFloat := MIN_MOD;
- maxFloat := MAX_MOD;
- end;
- WPM_TXT:
- begin
- maxFloat := MAX_WPM;
- minFloat := MIN_WPM;
- end;
- end;
-
- result := minFloat + (value - min) / (max - min) * (maxFloat - minFloat);
- fixedValue := X2Fix(result);
- end;
- procedure GetFixedString (fixedValue: Fixed;
- var fixedString: Str255);
- var
- aDecForm: DecForm;
- tempExtended: extended;
- begin
- tempExtended := Fix2X(fixedValue);
- aDecForm.style := FixedDecimal;
- aDecForm.digits := 2;
- num2str(aDecForm, tempExtended, DecStr(fixedString));
- end;
- procedure SetFixedTxtValue (setupDlg: DialogPtr;
- ctlH: ControlHandle;
- item: integer;
- var fixedValue: Fixed);
- var
- iHandle: Handle;
- iType: integer;
- iRect: Rect;
- value: integer;
- fixedString: Str255;
- begin
- GetDItem(setupDlg, item, iType, iHandle, iRect);
- value := GetCtlValue(ctlH);
- MapValue2Fixed(ctlH, value, item, fixedValue);
- GetFixedString(fixedValue, fixedString);
- SetIText(iHandle, fixedString);
- end; (* end SetFixedTxtValue *)
-
- function MapFixed2Value (slideH: ControlHandle;
- item: integer;
- fixedValue: Fixed): integer;
- var
- min: extended;
- max: extended;
- minFloat: extended;
- maxFloat: extended;
- valFloat: extended;
- begin
- case item of
- PITCH_SLIDE:
- begin
- minFloat := MIN_PITCH;
- maxFloat := MAX_PITCH;
- end;
- WPM_SLIDE:
- begin
- minFloat := MIN_WPM;
- maxFloat := MAX_WPM;
- end;
- VOL_SLIDE:
- begin
- minFloat := MIN_VOL;
- maxFloat := MAX_VOL;
- end;
- MOD_SLIDE:
- begin
- minFloat := MIN_MOD;
- maxFloat := MAX_MOD;
- end;
- end;
-
- min := GetCtlMin(slideH);
- max := GetCtlMax(slideH);
- valFloat := Fix2X(fixedValue);
- valFloat := min + (max - min) * ((valFloat - minFloat) / (maxFloat - minFloat));
- MapFixed2Value := Num2Integer(valFloat);
- end;
-
-
- procedure SetBiteFreqValue (setupDlg: DialogPtr;
- value: integer;
- undimmed: Boolean);
- var
- biteCtl: ControlHandle;
- iRect: Rect;
- iType: integer;
- begin
- GetDItem(setupDlg, BITE_FREQ_SLIDE, iType, Handle(biteCtl), iRect);
- SetCtlValue(biteCtl, value);
- if undimmed = TRUE then
- begin
- HiliteControl(biteCtl, 0);
- end
- else
- begin
- HiliteControl(biteCtl, 255);
- end;
- end;
-
- function HandleNonDialogEvent (theEvent: EventRecord;
- setupDlg: DialogPtr): integer;
- var
- whichWin: WindowPtr;
- iWinPart: integer;
- limitRect: Rect;
- menuChoice: LONGINT;
- begin
- HandleNonDialogEvent := 0;
- case theEvent.what of
- updateEvt:
- begin
- DrawControls(setupDlg);
- end;
- mouseDown:
- begin
- iWinPart := FindWindow(theEvent.where, whichWin);
- case iWinPart of
- inDesk:
- begin
- end;
- inMenuBar:
- begin
- menuChoice := MenuSelect(theEvent.where);
- HandleMenuChoice(menuChoice);
- if gDone = TRUE then
- HandleNonDialogEvent := 2;
- end;
- inSysWindow:
- begin
- end;
- inDrag:
- begin
- SetRect(limitRect, -32768, -32768, 32767, 32767);
- DragWindow(whichWin, theEvent.where, limitRect);
-
- end;
- inContent:
- begin
- end;
- end;
- end;
- otherwise
- begin
- end;
- end;
-
- end;
- function FindTextFromControl (setupDlg: DialogPtr;
- ctlH: ControlHandle): integer;
- var
- iType: integer;
- iRect: Rect;
- iHandle: ControlHandle;
- index: integer;
- result: integer;
- begin
- index := PITCH_SLIDE;
- result := 0;
- while (index <= VOL_SLIDE) and (result = 0) do { note that the bite freq. wont be found }
- begin
- GetDItem(setupDlg, index, iType, Handle(iHandle), iRect);
- if iHandle = ctlH then
- begin
- result := index + 4;
- end;
- index := index + 1;
- end;
- FindTextFromControl := result;
- end;
-
- procedure SetMyCtlActions (setupDlg: DialogPtr;
- item: integer;
- actProc: ProcPtr);
- var
- ctlH: ControlHandle;
- iType: integer;
- iRect: Rect;
-
- begin
- GetDItem(setupDlg, item, iType, Handle(ctlH), iRect);
- SetCtlAction(ctlH, actProc);
- end;
- function HandleDialogEvent (theEvent: EventRecord;
- setupDlg: DialogPtr): integer;
- var
- whichDlog: DialogPtr;
- itemHit: integer;
- value: integer;
- ctlH: ControlHandle;
- iRect: Rect;
- iType: integer;
- iErr: OSErr;
- locPoint: Point;
- doSelect, doHandle, doCancel: Boolean;
- theChar: CHAR;
- charCode: integer;
- tick: LONGINT;
- charPtr: Ptr;
- testString: Str255;
- begin
- HandleDialogEvent := 0;
- doSelect := TRUE;
- doHandle := FALSE;
- doCancel := FALSE;
- case theEvent.what of
- mouseDown:
- begin
- locPoint := theEvent.where;
- GlobalToLocal(locPoint);
- itemHit := FindControl(locPoint, setupDlg, ctlH);
- end;
- keyDown, autoKey:
- begin
- theChar := CHR(BitAnd(theEvent.message, charCodeMask));
- if (BitAnd(theEvent.modifiers, cmdKey) <> 0) then
- begin
- HandleMenuChoice(MenuKey(theChar));
- if (gDone) or (ORD(theChar) = 46) then
- begin
- HandleDialogEvent := 2;
- doCancel := TRUE;
- end;
- doSelect := FALSE;
- end
- else
- begin
- charPtr := @theChar;
- charCode := CharType(charPtr, 1);
- if ORD(theChar) = 13 then
- begin
- doSelect := FALSE;
- GetDItem(setupDlg, OK_BUT, iType, Handle(ctlH), iRect);
- if ctlH^^.contrlHilite <> 255 then
- begin
- HandleDialogEvent := 1;
- HiliteControl(ctlH, 1);
- Delay(5, tick);
- HiliteControl(ctlH, 1);
- end;
- end
-
- else if ORD(theChar) = 27 then
- begin
- HandleDialogEvent := 2;
- doSelect := FALSE;
- doCancel := TRUE;
- end
- else
- begin
- doSelect := FALSE;
- end;
-
- end;
- end;
- end;
- if doCancel then
- begin
- GetDItem(setupDlg, CAN_BUT, iType, Handle(ctlH), iRect);
- HiliteControl(ctlH, 1);
- Delay(5, tick);
- HiliteControl(ctlH, 1);
- end;
- if doSelect then
- begin
- if DialogSelect(theEvent, whichDlog, itemHit) = TRUE then
- doHandle := TRUE;
- end;
- if doHandle then
- begin
- if whichDlog = setupDlg then
- begin
- GetDItem(setupDlg, itemHit, iType, Handle(ctlH), iRect);
- case itemHit of
- OK_BUT:
- begin
- HandleDialogEvent := 1;
- end;
- CAN_BUT:
- begin
- HandleDialogEvent := 2;
- end;
- HOUR_CHECK:
- begin
- NegateCheckItem(setupDlg, HOUR_CHECK, gLocalPrefs.sayHours);
- end;
- DAY_CHECK:
- begin
- NegateCheckItem(setupDlg, DAY_CHECK, gLocalPrefs.sayDays);
- end;
- USE_DAEMON_CHECK:
- begin
- NegateCheckItem(setupDlg, USE_DAEMON_CHECK, gLocalPrefs.useDaemon);
- ActivateForDaemon(setupDlg, gLocalPrefs.useDaemon);
- end;
- KEEP_VOICE_CHECK:
- begin
- NegateCheckItem(setupDlg, KEEP_VOICE_CHECK, gLocalPrefs.keepVoice);
- end;
- BITES_CHECK:
- begin
- NegateCheckItem(setupDlg, BITES_CHECK, gLocalPrefs.sayBites);
- SetBiteFreqValue(setupDlg, gLocalPrefs.biteFreq, gLocalPrefs.sayBites);
- end;
- BITE_FREQ_SLIDE:
- begin
- gLocalPrefs.biteFreq := GetCtlValue(ControlHandle(ctlH));
- end;
- PITCH_SLIDE:
- begin
- SetFixedTxtValue(setupDlg, ctlH, PITCH_TXT, gLocalPrefs.pitch);
- end;
- WPM_SLIDE:
- begin
- SetFixedTxtValue(setupDlg, ctlH, WPM_TXT, gLocalPrefs.wpm);
- end;
- VOL_SLIDE:
- begin
- SetFixedTxtValue(setupDlg, ctlH, VOL_TXT, gLocalPrefs.volume);
- end;
- MOD_SLIDE:
- begin
- SetFixedTxtValue(setupDlg, ctlH, MOD_TXT, gLocalPrefs.modulation);
- end;
- VOICE_POP:
- begin
- value := GetCtlValue(ctlH);
- iErr := GetIndVoice(value, @gLocalPrefs.voice);
- end;
- TEST_BUT:
- begin
- GetIndString(testString, 128, 3);
- if gLocalPrefs.useDaemon then
- begin
- Talk2Daemon(gLocalPrefs, testString);
- end
- else
- begin
- iErr := SpeakString(testString);
- end;
- end;
- end;
- end;
- end;
- end;
-
-
- function HandleTheSetup (setupDlg: DialogPtr): Boolean;
- var
- done: integer;
- theEvent: EventRecord;
- begin
- done := 0;
- while (done = 0) do
- begin
- gTrackTime := TickCount;
- if (GetNextEvent(everyEvent, theEvent)) then
- begin
- if (IsDialogEvent(theEvent)) then
- begin
- done := HandleDialogEvent(theEvent, setupDlg);
- end
- else
- begin
- done := HandleNonDialogEvent(theEvent, setupDlg);
- end;
- end;
- end;
- if done = 1 then
- begin
- HandleTheSetup := TRUE;
- end
- else
- begin
- HandleTheSetup := FALSE;
- end;
- end;
-
- procedure BuildVoiceMenu (setupDlg: DialogPtr;
- var voice: VoiceSpec);
- var
- numVoices: integer;
- i: integer;
- iErr: OSErr;
- iType: integer;
- iHandle: ControlHandle;
- iRect: Rect;
- privData: PopPrivateH;
- menuH: MenuHandle;
- aVoiceSpec: VoiceSpec;
- info: VoiceDescription;
- voiceFound: Boolean;
- begin
- info.length := sizeof(VoiceDescription);
- info.reserved1 := 0;
- info.reserved2 := 0;
- info.reserved3 := 0;
- info.reserved4 := 0;
- GetDItem(setupDlg, VOICE_POP, iType, Handle(iHandle), iRect);
- privData := PopPrivateH(iHandle^^.contrlData);
- menuH := privData^^.mHandle;
- iErr := CountVoices(numVoices);
- if iErr = 0 then
- begin
- i := 1;
- voiceFound := FALSE;
- while (i <= numVoices) and (iErr = 0) do
- begin
- iErr := GetIndVoice(i, @aVoiceSpec);
- if iErr = 0 then
- begin
- iErr := GetVoiceDescription(@aVoiceSpec, @info, sizeof(VoiceDescription));
- if iErr = 0 then
- begin
- AppendMenu(menuH, info.name);
- if (voice.creator = aVoiceSpec.creator) and (voice.id = aVoiceSpec.id) then
- begin
- voiceFound := TRUE;
- iHandle^^.contrlValue := i;
- end
- else if (i = numVoices) and (voiceFound = FALSE) then
- begin
- iHandle^^.contrlValue := i;
- iErr := MakeVoiceSpec(aVoiceSpec.creator, aVoiceSpec.id, @voice);
- end;
- end;
- end;
- i := i + 1;
- end;
- end;
-
- end;
-
-
- procedure SetSlideValue (setupDlg: DialogPtr;
- item: integer;
- itemText: integer;
- fixedValue: Fixed);
- var
- iType: integer;
- slideH: ControlHandle;
- textH: Handle;
- iRect: Rect;
- intValue: integer;
- fixedString: Str255;
- begin
- GetDItem(setupDlg, item, iType, Handle(slideH), iRect);
- GetDItem(setupDlg, itemText, iType, textH, iRect);
- intValue := MapFixed2Value(slideH, item, fixedValue);
- SetCtlValue(slideH, intValue);
- GetFixedString(fixedValue, fixedString);
- SetIText(textH, fixedString);
- end;
- procedure TrackASlide (item: integer);
- var
- setupDlg: DialogPtr;
- newValue: Fixed;
- theSlide: ControlHandle;
- iRect: Rect;
- iType, value: integer;
- fixedString: Str255;
- textH: Handle;
- begin
- GetPort(setupDlg);
- GetDItem(setupDlg, item, iType, Handle(theSlide), iRect);
- value := GetCtlValue(theSlide);
- MapValue2Fixed(theSlide, value, item + 4, newValue);
- GetFixedString(newValue, fixedString);
- GetDItem(setupDlg, item + 4, iType, textH, iRect);
- SetIText(textH, fixedString);
- end;
- procedure TrackPitch;
- begin
- TrackASlide(PITCH_SLIDE);
- end;
- procedure TrackRate;
- begin
- TrackASlide(WPM_SLIDE);
- end;
- procedure TrackVolume;
- begin
- TrackASlide(VOL_SLIDE);
- end;
- procedure TrackMod;
- begin
- TrackASlide(MOD_SLIDE);
- end;
-
- procedure SetupTheDialog (setupDlg: DialogPtr;
- prefs: PrefRecord);
- var
- hIntl: Handle;
- begin
- hIntl := IUGetIntl(0);
- if hIntl <> nil then
- gPeriod := char(hIntl^^);
- HPurge(hIntl);
- SetCheckItem(setupDlg, DAY_CHECK, prefs.sayDays);
- SetCheckItem(setupDlg, HOUR_CHECK, prefs.sayHours);
- SetCheckItem(setupDlg, BITES_CHECK, prefs.sayBites);
- SetCheckItem(setupDlg, USE_DAEMON_CHECK, prefs.useDaemon);
- SetCheckItem(setupDlg, KEEP_VOICE_CHECK, prefs.keepVoice);
- SetBiteFreqValue(setupDlg, prefs.biteFreq, prefs.sayBites);
- BuildVoiceMenu(setupDlg, prefs.voice);
- SetSlideValue(setupDlg, PITCH_SLIDE, PITCH_TXT, prefs.pitch);
- SetSlideValue(setupDlg, WPM_SLIDE, WPM_TXT, prefs.wpm);
- SetSlideValue(setupDlg, VOL_SLIDE, VOL_TXT, prefs.volume);
- SetSlideValue(setupDlg, MOD_SLIDE, MOD_TXT, prefs.modulation);
- SetMyCtlActions(setupDlg, PITCH_SLIDE, @TrackPitch);
- SetMyCtlActions(setupDlg, WPM_SLIDE, @TrackRate);
- SetMyCtlActions(setupDlg, VOL_SLIDE, @TrackVolume);
- SetMyCtlActions(setupDlg, MOD_SLIDE, @TrackMod);
-
- ActivateForDaemon(setupDlg, prefs.useDaemon);
- end;
-
- procedure GetNewPreferences;
- var
- setupDlg: DialogPtr;
- iErr: OSErr;
- dummy: Boolean;
- begin
- BlockMove(@gPreferences, @gLocalPrefs, sizeof(PrefRecord));
- setupDlg := GetNewDialog(SETUP_ID, nil, DialogPtr(-1));
- if setupDlg <> nil then
- DisposDialog(setupDlg);
- setupDlg := GetNewDialog(SETUP_ID, nil, DialogPtr(-1));
-
-
- if setupDlg <> nil then
- begin
- SetupTheDialog(setupDlg, gLocalPrefs);
- HideWindow(gWindow);
- KillMyGWorlds;
- ShowWindow(setupDlg);
- SetPort(setupDlg);
- ClipRect(setupDlg^.portRect);
- if HandleTheSetup(setupDlg) then
- begin
- BlockMove(@gLocalPrefs, @gPreferences, sizeof(PrefRecord));
- end;
- DisposDialog(setupDlg);
- dummy := InitMyGWorlds;
-
- ShowWindow(gWindow);
- SetPort(gWindow);
- SelectWindow(gWindow);
- DrawTheWatch;
- end; (* use of setup dialog *)
- end;
- end.